import type { AccountOption } from '@/api/user/types';
import storage from '@/utils/storage';

const ACCOUNT_CACHE_KEY = 'multi_account_cache';
const MERCHANTS_CACHE_KEY = 'multiple_merchants_cache';
const CACHE_EXPIRE_TIME = 24 * 60 * 60 * 1000; // 24小时过期

interface AccountCache {
  accounts: AccountOption[];
  timestamp: number;
  mobile: string; // 关联的手机号
  password: string; // 关联的密码
}

interface MerchantsCache {
  merchants: Array<{
    id: number;
    name: string;
    mobile: string;
    dangkou_name: string;
  }>;
  timestamp: number;
  mobile: string; // 关联的手机号
}

/**
 * 多账户缓存管理工具
 */
export class AccountCacheManager {
  /**
   * 保存多账户信息到缓存
   * @param mobile 手机号
   * @param accounts 账户列表
   */
  static saveAccounts(mobile: string, accounts: AccountOption[], password: string): void {
    const cacheData: AccountCache = {
      accounts,
      timestamp: Date.now(),
      mobile,
      password,
    };
    storage.setJSON(ACCOUNT_CACHE_KEY, cacheData);
  }

  /**
   * 从缓存获取多账户信息
   * @param mobile 手机号
   * @returns 账户列表或null
   */
  static getAccounts(mobile: string): AccountOption[] | null {
    try {
      const cacheData: AccountCache = storage.getJSON(ACCOUNT_CACHE_KEY);

      if (!cacheData) {
        return null;
      }

      // 检查手机号是否匹配
      if (cacheData.mobile !== mobile) {
        this.clearCache();
        return null;
      }

      // 检查是否过期
      const now = Date.now();
      if (now - cacheData.timestamp > CACHE_EXPIRE_TIME) {
        this.clearCache();
        return null;
      }

      return cacheData.accounts;
    }
    catch (error) {
      console.error('获取账户缓存失败:', error);
      this.clearCache();
      return null;
    }
  }

  /**
   * 检查是否有缓存的多账户信息
   * @param mobile 手机号
   * @returns 是否有有效缓存
   */
  static hasCache(mobile: string): boolean {
    return this.getAccounts(mobile) !== null;
  }

  /**
   * 清除账户缓存
   */
  static clearCache(): void {
    storage.remove(ACCOUNT_CACHE_KEY);
    storage.remove(MERCHANTS_CACHE_KEY);
  }

  /**
   * 获取缓存信息（用于调试）
   */
  static getCacheInfo(): AccountCache | null {
    try {
      return storage.getJSON(ACCOUNT_CACHE_KEY);
    }
    catch (error) {
      return null;
    }
  }

  /**
   * 检查缓存是否过期
   * @param mobile 手机号
   * @returns 是否过期
   */
  static isCacheExpired(mobile: string): boolean {
    try {
      const cacheData: AccountCache = storage.getJSON(ACCOUNT_CACHE_KEY);

      if (!cacheData || cacheData.mobile !== mobile) {
        return true;
      }

      const now = Date.now();
      return now - cacheData.timestamp > CACHE_EXPIRE_TIME;
    }
    catch (error) {
      return true;
    }
  }

  /**
   * 保存多商户信息到缓存
   * @param mobile 手机号
   * @param merchants 商户列表
   */
  static saveMerchants(mobile: string, merchants: Array<{
    id: number;
    name: string;
    mobile: string;
    dangkou_name: string;
  }>): void {
    const cacheData: MerchantsCache = {
      merchants,
      timestamp: Date.now(),
      mobile,
    };
    storage.setJSON(MERCHANTS_CACHE_KEY, cacheData);
  }

  /**
   * 从缓存获取多商户信息
   * @param mobile 手机号
   * @returns 商户列表或null
   */
  static getMerchants(mobile: string): Array<{
    id: number;
    name: string;
    mobile: string;
    dangkou_name: string;
  }> | null {
    try {
      const cacheData: MerchantsCache = storage.getJSON(MERCHANTS_CACHE_KEY);

      if (!cacheData) {
        return null;
      }

      // 检查手机号是否匹配
      if (cacheData.mobile !== mobile) {
        storage.remove(MERCHANTS_CACHE_KEY);
        return null;
      }

      // 检查是否过期
      const now = Date.now();
      if (now - cacheData.timestamp > CACHE_EXPIRE_TIME) {
        storage.remove(MERCHANTS_CACHE_KEY);
        return null;
      }

      return cacheData.merchants;
    }
    catch (error) {
      console.error('获取商户缓存失败:', error);
      storage.remove(MERCHANTS_CACHE_KEY);
      return null;
    }
  }

  /**
   * 检查是否有缓存的多商户信息
   * @param mobile 手机号
   * @returns 是否有有效缓存
   */
  static hasMerchantsCache(mobile: string): boolean {
    return this.getMerchants(mobile) !== null;
  }
}

// 导出默认实例
export default AccountCacheManager;
